﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Teal.Compiler {

    /// <summary>
    /// 表示一个用于生成 C 源码的上下文。
    /// </summary>
    public sealed class CCodeGenContext {

        TextWriter _writer;

        public CCodeGenContext(TextWriter writer) {
            _writer = writer;
        }

        bool _needWriteIndents;

        public int indent;

        public void writeIndents() {
            if (_needWriteIndents) {
                _needWriteIndents = false;
                for (int i = 0; i < indent; i++) {
                    _writer.Write('\t');
                }
            }
        }

        public void write(string code) {
            writeIndents();
            _writer.Write(code);
        }

        public void write(TypeReference type) {
            writeIndents();
            _writer.Write(type.fullName);
        }

        public void write(char value) {
            writeIndents();
            _writer.Write(value);
        }

        public void writeLine() {
            _writer.WriteLine();
            _needWriteIndents = true;
        }

        public void writeLine(string code) {
            writeIndents();
            _writer.WriteLine(code);
            _needWriteIndents = true;
        }

        public void writeLine(char code) {
            writeIndents();
            _writer.WriteLine(code);
            _needWriteIndents = true;
        }

    }
}
